/**
 *
 */
package com.ybkj.daijia.api.driver.v4;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.api.util.ErrorConstantV4;
import com.ybkj.daijia.server.model.Area;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.DriverLogin;
import com.ybkj.daijia.server.service.AreaService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverLoginService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.ZhuanxianService;
import com.ybkj.daijia.server.zhuanxian.Line;
import com.ybkj.daijia.server.zhuanxian.Order;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author bo
 *
 */
@Controller("ZhuanxianProcessControllerV4")
@RequestMapping(value = "driver/api/rest/v4")
public class ZhuanxianProcessController {

    private static final Logger logger = LoggerFactory.getLogger(ZhuanxianProcessController.class);

    @Autowired
    private DriverService driverService;

    @Autowired
    private ZhuanxianService zhuanxianService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private AreaService areaService;


    @Autowired
    private DriverLoginService driverLoginService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    /**
     * 获取单个订单数据
     * @param employToken
     * @param orderId
     * @return
     */
    @RequestMapping(value = "getZhuanxianOrderInfo")
    public @ResponseBody
    ResultTransport getZhuanxianOrderInfo(String employToken, Long orderId) {

        logger.debug("recv employToken:{},orderId:{}", employToken, orderId);

        if (StringUtils.isBlank(employToken) || null == orderId) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        DriverLogin driverLogin = driverLoginService.selectByToken(employToken);

        if (null == driverLogin) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_ERROR);
        }

        if (System.currentTimeMillis() >= driverLogin.getDeathDate().getTime()) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_TIME_ERROR);
        }

        Driver driver = driverService.findOne(driverLogin.getDriverId());

        if (null == driver) {
            return ResultTransport.getErrorBy(ErrorConstantV4.EMPLOY_NOT_EXISTS);
        }

        if (driver.getIsFreeze()) {
            return ResultTransport.getErrorBy(ErrorConstantV4.EMPLOY_IS_FREEZE_ERROR);
        }

        Order order = zhuanxianService.findOne(orderId);

        if (null == order) {
            return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_NOT_EXISTS_ERROR);
        }

        try {

            Map<String, Object> zhuanxianMap = new HashMap<String, Object>();
            zhuanxianMap.put("orderId", order.getId());
            zhuanxianMap.put("zxOrderType", order.getOrderType());
            zhuanxianMap.put("lineId", order.getLineId());
            zhuanxianMap.put("lineName", order.getLineName());
            zhuanxianMap.put("peopleNumber", order.getPeopleNumber());
            zhuanxianMap.put("budgetPay", order.getBudgetPay());
            zhuanxianMap.put("orderNumber", order.getOrderNo());
            zhuanxianMap.put("time", order.getServerTime().getTime());
            zhuanxianMap.put("status", order.getStatus());
            zhuanxianMap.put("companyId", order.getCompanyId());
            zhuanxianMap.put("orderType", "zhuanxian");
            zhuanxianMap.put("startLng", order.getStartLng());
            zhuanxianMap.put("startLat", order.getStartLat());
            zhuanxianMap.put("endLng", order.getEndLng());
            zhuanxianMap.put("endLat", order.getEndLat());
            zhuanxianMap.put("fromPlace", order.getStartAddress());
            zhuanxianMap.put("toPlace", order.getEndAddress());
            zhuanxianMap.put("passengerPhone",
                StringUtils.isNotBlank(order.getShowPhone()) ? order.getShowPhone()
                    : order.getPassengerPhone());
            zhuanxianMap.put("passengerName",
                StringUtils.isNotBlank(order.getShowName()) ? order.getShowName()
                    : order.getPassengerName());
            zhuanxianMap.put("companyAbbreviation", order.getCompanyName());
            zhuanxianMap.put("takeDeliveryName", order.getTakeDeliveryName());
            zhuanxianMap.put("takeDeliveryPhone", order.getTakeDeliveryPhone());
            zhuanxianMap.put("treatment", order.getTreatment());
            if (order.getTreatment()) {
                zhuanxianMap.put("realCash", order.getRealPay());
                zhuanxianMap.put("guoluMoney", order.getGuoluMoney());
                zhuanxianMap.put("yuanchengMoney", order.getYuanchengMoney());
                zhuanxianMap.put("otherMoney", order.getOtherMoney());
                zhuanxianMap.put("couponMoney", order.getCouponMoney());
                zhuanxianMap.put("mileage", order.getMileage());
                zhuanxianMap.put("waitedTime", order.getStartWait());
                zhuanxianMap.put("travelTime", order.getRunTime());
                zhuanxianMap.put("startPrice", order.getStartPrice());
                zhuanxianMap.put("mileagePrice", order.getMileagePrice());
                zhuanxianMap.put("waitPrice", order.getWaitPrice());
                zhuanxianMap.put("runTimePrice", order.getRunTimePrice());
            }
            String memo = "";
            if (Order.TYPE_JIHUO.equals(order.getOrderType())) {
                memo = "收货人:(" + order.getTakeDeliveryName() + ") 、  收货人电话:(" + order
                    .getTakeDeliveryPhone() + ")";
            }
            memo += (((StringUtils.isNotBlank(order.getMemo())) ? order.getMemo() + ";" : ""));
            zhuanxianMap.put("memo", memo);
            Line line = zhuanxianService.findByLine(order.getLineId());
            if (order.getOrderType().equals(com.ybkj.daijia.server.zhuanxian.Order.TYPE_BAOCHE)) {
                Area bcarea = areaService.findOneWithCharge(line.getBaocheAreaId());
                zhuanxianMap
                    .put("chargeStartTimes", bcarea == null ? null : bcarea.getChargeStartTimes());
            } else if (order.getOrderType()
                .equals(com.ybkj.daijia.server.zhuanxian.Order.TYPE_JIHUO)) {
                Area jharea = areaService.findOneWithCharge(line.getJihuoAreaId());
                zhuanxianMap
                    .put("chargeStartTimes", jharea == null ? null : jharea.getChargeStartTimes());
            } else if (order.getOrderType()
                .equals(com.ybkj.daijia.server.zhuanxian.Order.TYPE_PINCHE)) {
                Area pcarea = areaService.findOneWithCharge(line.getAreaId());
                zhuanxianMap
                    .put("chargeStartTimes", pcarea == null ? null : pcarea.getChargeStartTimes());
            } else {
                zhuanxianMap.put("chargeStartTimes", null);
            }
            zhuanxianMap.put("showPhone", order.getShowPhone());
            zhuanxianMap.put("showName", order.getShowName());

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(zhuanxianMap);
            return instance;

        } catch (Exception e) {
            logger.error("api v4 driver getZhuanxianOrderInfo error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }

    }


}
